package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.model.PersonalInfoUpdateVo;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;

public class StaffViewModel extends BaseViewModel  {

    private MediatorLiveData<PageResponse<PersonalInfo>> staffList;

    private MediatorLiveData<BaseResponse<PersonalInfo>>staff;
    private MediatorLiveData<BaseResponse<PersonalInfo>>saveStaff;
    private MediatorLiveData<BaseResponse<PersonalInfo>>addStaff;





    private DcrmApi dcrmApi;
    private Retrofit mRetrofit;
    public StaffViewModel(Retrofit retrofit){
        this.mRetrofit=retrofit;
        dcrmApi=mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<PersonalInfo>> getStaff(long userId) {
        staff=creatLiveData(staff);
        dcrmApi.getStaffById(userId).enqueue(new CallBack<>(this,staff));
        return staff;
    }

    public MediatorLiveData<BaseResponse<PersonalInfo>> saveStaff(PersonalInfoUpdateVo personalInfoUpdateVo) {
        Map<String,String> map=new HashMap<>();
        try {
            map=Tool.objectToMap(personalInfoUpdateVo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        saveStaff=creatLiveData(saveStaff);
        dcrmApi.updatePersonalInfo(map).enqueue(new CallBack<>(this,saveStaff));
        return saveStaff;
    }

    public MediatorLiveData<PageResponse<PersonalInfo>> getStaffList(int page , int size) {
        staffList=creatLiveData(staffList);
        dcrmApi.getStaff(page,size)
                .enqueue(new CallBack<>(this,staffList));
        return staffList;
    }
    public MediatorLiveData<BaseResponse<PersonalInfo>> addStaff(PersonalInfoUpdateVo personalInfo){
        addStaff=creatLiveData(addStaff);
        dcrmApi.addStaff(personalInfo).enqueue(new CallBack<>(this,addStaff));
        return addStaff;
    }




    public MediatorLiveData<BaseResponse<PersonalInfo>> getStaff() {
        staff=creatLiveData(staff);
        return staff;
    }


    public MediatorLiveData<BaseResponse<PersonalInfo>> getSaveStaff() {
        saveStaff=creatLiveData(saveStaff);
        return saveStaff;
    }


}
